﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MyFinanceManager.Objects;
using MyFinanceManager.Controls;

namespace MyFinanceManager
{
    public partial class ucCategory : FiringUserControl
    {
        public string Value { get; private set; }
        public int ValueAsInt { get; private set; }
        private Dictionary<string, int> items = new Dictionary<string, int>();
        private Repository rep;

        public ucCategory(Repository rep)
        {
            this.rep = rep;
            this.Value = "";
            this.ValueAsInt = -1;
            InitializeComponent();

            List<CatInfo> ergebnis = Helper.GetSubCatInfos(0, rep.CatInfos);

            WriteNewText(0, this.button1, ergebnis);
            WriteNewText(1, this.button2, ergebnis);
            WriteNewText(2, this.button3, ergebnis);
            WriteNewText(3, this.button4, ergebnis);
            WriteNewText(4, this.button5, ergebnis);
            WriteNewText(5, this.button6, ergebnis);
            WriteNewText(6, this.button7, ergebnis);
            WriteNewText(7, this.button8, ergebnis);
            WriteNewText(8, this.button9, ergebnis);
            WriteNewText(9, this.button10, ergebnis);
            WriteNewText(10, this.button11, ergebnis);
            WriteNewText(11, this.button12, ergebnis);
            WriteNewText(12, this.button13, ergebnis);
            WriteNewText(13, this.button14, ergebnis);
            WriteNewText(14, this.button15, ergebnis);
            WriteNewText(15, this.button16, ergebnis);
            WriteNewText(16, this.button17, ergebnis);
            WriteNewText(17, this.button18, ergebnis);
            WriteNewText(18, this.button19, ergebnis);
            WriteNewText(19, this.button20, ergebnis);
        }

        private void WriteNewText(int p, Button button, List<CatInfo> ergebnis)
        {
            if (ergebnis.Count > p)
            {
                button.Text = ergebnis[p].Text;
                if (String.IsNullOrEmpty(ergebnis[p].Tag))
                    button.Tag = null;
                button.Tag = ergebnis[p].Tag;
                button.Enabled = true;

                items.Add(button.Text, p + 1);
            }
            else
            {
                button.Text = string.Empty;
                button.Tag = null;
                button.Enabled = false;
            }
        }

        /*
         * Wurde früher benutzt um Subkategorien zu realiseren
         * 
        private void contextMenu1_Popup(object sender, EventArgs e)
        {
            this.ContextMenu.MenuItems.Clear();

            if (sender is ContextMenu)
            {
                Button senderBt = (sender as ContextMenu).SourceControl as Button;
                if (senderBt != null)
                {
                    switch (senderBt.Text)
                    {
                        #region 1. Reihe
                        case "PC":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zubehör für Notebook" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zubehör für Server" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zubehör für SmartPhone" });
                            break;
                        case "Versicherung":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Lebensversicherung" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Krankenversicherung" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Autoversicherung" });
                            break;
                        case "Miete":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Strom" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Wärme" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Garage" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Sonstiges" });
                            break;
                        case "Buch":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "CD" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "DVD" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Buch" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zeitschrift" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zeitung" });
                            break;
                        #endregion

                        #region 2. Reihe
                        //case "Kino":
                        //    break;
                        case "Essen":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Essen (getrennt)" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Essen (zusammen)" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Fastfood" });
                            break;
                        case "Kaffee":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Kneipe" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Bar" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Club" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Frühstück" });
                            break;
                        case "Urlaub":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Konzert" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Hotel" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Mietwagen" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Eintritt" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Erinnerungen" });
                            break;
                        #endregion

                        #region 3. Reihe
                        case "Kleid.":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "T-Shirts" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Polo" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Hemd" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Anzug" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Hose" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Jacke" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Unterwäsche" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Sommersachen" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Wintersachen" });
                            break;
                        case "Schuh":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Sneakers" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Lederschuhe" });
                            break;
                        case "IKEA":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Möbel" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Blumen" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Geschirr" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Kleinzeugs" });
                            break;
                        case "Sport":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Sportgeräte" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Sporteintritte" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Sportveranstaltungen" });
                            break;
                        #endregion

                        #region 4. Reihe
                        //case "Nahr.":
                        //    break;
                        //case "Süßig.":
                        //    break;
                        //case "Trink.":
                        //    break;
                        case "Gesch.":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Mitbringsel" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Geburtstagsgeschenk" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Weihnachtsgeschenk" });
                            break;
                        #endregion

                        #region 5. Reihe
                        case "Auto":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Benzin" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Autowartung" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Autopflege" });
                            break;
                        case "Reisen":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Bus" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Flugzeug" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Bahn" });
                            break;
                        case "Haush.":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Reinigungsmittel" });
                            break;
                        case "Pflege":
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Duschgel" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Deo" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Rasierzeug" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zahnpasta" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zubehör für Zahnbürste" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Zubehör für Rasierapparat" });
                            this.contextMenu1.MenuItems.Add(new MenuItem { Text = "Rasierklingen" });
                            break;
                        #endregion

                        default:
                            break;
                    }
                }
            }

            foreach (MenuItem item in this.contextMenu1.MenuItems)
            {
                item.Click += new EventHandler(item_Click);
            }

        }
        */

        /// <summary>
        /// Handles the Click event of the item control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void item_Click(object sender, EventArgs e)
        {
            Control senderControl = sender as Control;
            if (senderControl != null)
            {
                this.Value = senderControl.Text;

                if (senderControl.Tag != null && !String.IsNullOrEmpty(senderControl.Tag.ToString()))
                    this.Value = senderControl.Tag.ToString();

                if (this.items.ContainsKey(senderControl.Text))
                    this.ValueAsInt = this.items[senderControl.Text];
                else
                    this.ValueAsInt = -1;
            }
            // Gehört noch zur alten Lösung mit Kontextmenü
            // else if (sender is MenuItem)
            //    this.Value = (sender as MenuItem).Text;

            FireNewValueAvailable(this.Value);
        }
    }
}
